<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery.form/3.24/jquery.form.js"></script>
    <script src="https://cdn.bootcss.com/jquery-cookie/1.3.0/jquery.cookie.js"></script>
    <link href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/assets/style/app.css"/>

</head>
<body>


<div class="userform-wrapper">
    <form class="form-inline login" novalidate id="loginForm" method="post">
        <div class="form-group">
            <label for="nickname" class="sr-only">名字</label>
            <div class="input-group">
                <div class="input-group-addon"><span class="glyphicon glyphicon-user">名字</span></div>
                <input type="text" class="form-control" id="nickName" name="nickName" placeholder="你的名字" required/>
            </div>
        </div>
        <button type="button" class="btn btn-primary" onclick="login()">登录</button>
        <p>输入一个用户名登录进去就完事了哦</p>
    </form>
</div>
<script>
    function login() {
        $.ajax({
            url: '/login',
            type: 'post',
            dataType: 'json',
            data:JSON.stringify($("#loginForm").serializeObject()),
            contentType: "application/json",
            success: function (data) {
                if (data.status == 200) {
                    alert(data.msg);
                    $.cookie('authToken',data.data.token,{expires:4});
                    window.location.href = "/chatUI/chat.html";
                } else {
                    alert(data.msg);
                }
            }
        });
    }

    /**
     * 自动将form表单封装成json对象
     */
    $.fn.serializeObject = function() {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [ o[this.name] ];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
</script>
</body>
</html>